import { Component, OnInit, OnDestroy } from '@angular/core';

import { GlobalState } from '../../../global.state';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/interval';
import { Subscription } from 'rxjs/Subscription';

@Component({
  selector: 'ba-content-top',
  styles: [require('./baContentTop.scss')],
  template: require('./baContentTop.html'),
})
export class BaContentTop implements OnInit, OnDestroy {
  private directToChangePass: string;
  public activePageTitle: string = '';
  private timerSub: Subscription;
  private homeLink: string;
  private currentTime: Date;
  private userType: string;

  constructor(private _state: GlobalState) {
    this.homeLink = '/' + this._state.userType + '/dashboard';
    this._state.subscribe('menu.activeLink', (activeLink) => {
      if (activeLink) {
        this.activePageTitle = activeLink.title;
      }
    });
  }

  ngOnInit() {
    let timer = Observable.interval(1000);
    this.timerSub = timer.subscribe(() => this.currentTime = new Date() );
    switch (this._state.userType) {
      case 'user':
        this.userType = '个人';
        this.directToChangePass = '/pages/user/changepass';
        break;
      case 'company':
        this.userType = '企业';
        this.directToChangePass = '/pages/company/changepass';
        break;
      case 'asso':
        this.userType = '协会';
        break;
      default:
        this.userType = '用户';
    }
  }

  ngOnDestroy() {
    if (this.timerSub !== null) {
      this.timerSub.unsubscribe();
    }
  }
}
